package com.social.controller;

import com.mzt.logapi.starter.annotation.LogRecord;
import com.social.constant.LogConst;
import com.social.pojo.Petition;
import com.social.pojo.Result;
import com.social.pojo.vto.PetitonVO;
import com.social.service.PetitionService;
import com.social.utils.annotation.RateLimiter;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.support.atomic.RedisAtomicInteger;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * @author zero
 * @version 1.0
 * @project demo
 * @description 信访
 * @date 2022/10/3 10:55:36
 */
@Api("PetitionController")
@RestController
@RequestMapping("/petition")
@Slf4j
public class PetitionController {
    @Resource
    private PetitionService petitionService;
    @Resource
    private RedisTemplate redisTemplate;

    @ApiOperation("发送短信")
    @GetMapping("/message")
    @RateLimiter(key = "test", time = 10, count = 2)
    public Result send(){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        String date = format.format(new Date());
        RedisAtomicInteger limitCounter = new RedisAtomicInteger("limitCounter", redisTemplate.getConnectionFactory());
        String str = date + " 累计访问次数：" + limitCounter.getAndIncrement();
        log.info(str);
        return petitionService.sendMessage();
    }

    @PostMapping("/add")
    @ApiOperation("新增信访")
    public Result add(@Valid @RequestBody Petition petition){
        return petitionService.add(petition);
    }


    @GetMapping("/detail")
    @ApiOperation("查看信访详情")
    public Result getDetail(@Valid @RequestParam Integer id){
       return petitionService.getPetitionById(id);
    }

    @GetMapping("/getall")
    @ApiOperation("查看所有信访")
    public Result getAll( @RequestParam Integer currentPage,
                          @RequestParam Integer pageSize){
        return petitionService.findAll(currentPage,pageSize);
    }

    @GetMapping("/user")
    @ApiOperation("查看用户的所有信访")
    public Result findAll(@RequestParam Integer currentPage,
                            @RequestParam Integer pageSize){
        return petitionService.findByUserId(currentPage,pageSize);
    }

    @GetMapping("/condition")
    @ApiOperation("条件查询信访")
    public Result findByCondition(@RequestBody PetitonVO petitionVO){
        return petitionService.findByCondition(petitionVO);
    }
    @PostMapping("/deal")
    @ApiOperation("处理信访")
    public Result dealPetition(@Valid @RequestBody Petition reply){
        return  petitionService.deal(reply);
    }

    @GetMapping("/findspeed")
    @ApiOperation("查看进度")
    @LogRecord(success = "{{#evaluate.userId}}新增了一条评价,评价打分是{{evaluate.score}}",
            type = LogConst.PETITION,bizNo = "{{evaluate.id}}")
    public Result findSpeedProcess(@RequestParam String findCode){return petitionService.findSpeedProcess(findCode);}

}